﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunnyFunction
{
    /* 螺旋矩阵
     * 给出一个 m*n 的矩阵
     * 按照顺时针的顺序，打印出所有的元素
     */
    class CyclePrint
    {
        public static void Print(int M, int N)
        {
            int[,] arr = new int[M, N];
            Random rand = new Random();

            for (int i = 0; i < M; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    arr[i, j] = rand.Next(1, 10);
                }
            }
            // 先打印一遍矩阵
            for (int j = 0; j < N; j++)
            {
                Console.Write("[");
                for (int i = 0; i < M; i++)
                {
                    Console.Write($" {arr[i, j]}");
                    if (i < M - 1)
                        Console.Write(",");
                    else
                        Console.Write(" ");
                }
                Console.WriteLine("]");
            }
            Console.WriteLine("顺时针结果为：");
            int x = 0;
            int y = 0;
            int num = 0;  // 0,→ 1, ↓  2,←，3，↑

            IList<int> list = new List<int>();
            while (true)
            {
                if (arr[x, y] == 0)
                    break;

                Console.Write(arr[x, y] + ",");
                arr[x, y] = 0;
                switch (num)
                {
                    case 0:
                        {
                            if (x < M - 1 && arr[x + 1, y] != 0)
                                x++;
                            else
                            {
                                num = 1;
                                y++;
                            }
                        }
                        break;
                    case 1:
                        {
                            if (y < N - 1 && arr[x, y + 1] != 0)
                            {
                                y++;
                            }
                            else
                            {
                                num = 2;
                                x--;
                            }
                        }
                        break;
                    case 2:
                        {
                            if (x > 0 && arr[x - 1, y] != 0)
                                x--;
                            else
                            {
                                y--;
                                num = 3;
                            }
                        }
                        break;
                    case 3:
                        {
                            if (y > 0 && arr[x, y - 1] != 0)
                                y--;
                            else
                            {
                                x++;
                                num = 0;
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
